package com.atguigu3.preparedstatement.crud;

import com.atguigu3.bean.Order;
import com.atguigu3.util.JDBCUtils;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.*;

/**
 * 针对于Order表的查询操作
 */
public class OrderForQuery {

    @Test
    public void testQuery2(){
        String sql = "select order_id orderId,order_name orderName,order_date orderDate from `order` where order_id = ?";
        Order order = orderForQuery(sql, 1);
        System.out.println(order);
    }

    /**
     * 通用的针对于order表的操作
     *  此时bean包下的Order中声明的属性名与sql中的不一致，
     *
     *  需要解决这一问题：java.lang.NoSuchFieldException: order_id
     *
     *  原因：在sql中表可以起别名，在JDBC中结果集需要处理这一问题
     *
     *  处理方式：
     *  ①将结果集的元数据ResultSetMetaData获取列名的方式由ColumnName改为
     *  ColumnLabel；
     *  ②和在MySQL中一样将列名改为与在java类中定义的属性名一致
     *
     *  说明：如果sql中的字段没有别名，getColumnLabel获取的就是列名
     * @return Order
     */
    public Order orderForQuery(String sql, Object... args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            //执行获取结果集
            rs = ps.executeQuery();
            //获取结果集符元数据
            ResultSetMetaData rsmd = rs.getMetaData();
            //获取列数
            int columnCount = rsmd.getColumnCount();
            if (rs.next()) {
                Order order = new Order();
                for (int i = 0; i < columnCount; i++) {
                    //获取每个列的列值:通过ResultSet
                    Object columnValue = rs.getObject(i + 1);

                    //通过ResultSetMetaDate
   //               获取每个列的列名:getColumnName ——>不推荐使用
                    //String columnName = rsmd.getColumnName(i + 1);

//                  获取每个列的别名：getColumnLabel
                    String columnLabel = rsmd.getColumnLabel(i + 1);

                    //通过反射，将指定名columnName的属性赋值为指定的值columnValue
                    Field field = Order.class.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(order, columnValue);
                }

                return order;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtils.closeResource(conn, ps, rs);
        }
        return null;
    }


    @Test
    public void testQuery1() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "select order_id,order_name,order_date from `order` where order_id = ?";
            ps = conn.prepareStatement(sql);
            ps.setObject(1, 1);
            rs = ps.executeQuery();
            if (rs.next()) {
                int id = (int) rs.getObject(1);
                String name = (String) rs.getObject(2);
                Date date = (Date) rs.getObject(3);

                Order order = new Order(id, name, date);
                System.out.println(order);
            }
        } catch (Exception e) {
           e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, ps, rs);
        }
    }


}
